package com.fan.leetcode.everydaycoding;

import java.util.Stack;

/**
 * @Author: 80305082
 * @Date: 2021/3/8
 * @Description: 两个栈实现队列
 */
public class TwoThreeTwo {

    private static int front;

    private static Stack<Integer> in;
    private static Stack<Integer> out;

    /**
     * Initialize your data structure here.
     */
    public TwoThreeTwo() {
        in = new Stack<>();
        out = new Stack<>();
    }

    /**
     * Push element x to the back of queue.
     */
    public void push(int x) {
        if (in.isEmpty()) {
            front = x;
        }
        in.push(x);
    }

    /**
     * Removes the element from in front of queue and returns that element.
     */
    public int pop() {
        if (out.isEmpty()) {
            while (!in.isEmpty()) {
                out.push(in.pop());
            }
        }
        return out.pop();
    }

    /**
     * Get the front element.
     */
    public int peek() {
        if (!out.isEmpty()) {
            return out.peek();
        }
        return front;
    }

    /**
     * Returns whether the queue is empty.
     */
    public boolean empty() {
        return in.isEmpty() && out.isEmpty();
    }
}


/**
 * Your MyQueue object will be instantiated and called as such:
 * MyQueue obj = new MyQueue();
 * obj.push(x);
 * int param_2 = obj.pop();
 * int param_3 = obj.peek();
 * boolean param_4 = obj.empty();
 */
